﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using BlogPlat.Model;
using BlogPlat.Model.Abstract;
using BlogPlat.Model.Repositories;
using System.Text.RegularExpressions;

namespace BlogPlat.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin,author")]
    public class CategoryController : Controller
    {        
        private IRepository<Category> _repository;

        public CategoryController(IRepository<Category> repository) 
        {
            _repository = repository;
        }
        
        public ActionResult Index()
        {
            return View(_repository.List());            
        }

        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Category newCategory)
        {
            if (ModelState.IsValid)
            {
                if (newCategory.ParentID == -1)
                    newCategory.ParentID = null;

                newCategory.FriendlyUrl = newCategory.FriendlyUrl.Replace(" ", "-");
                newCategory.FriendlyUrl = Regex.Replace(newCategory.FriendlyUrl, "[\"$&+,/:;=?@]", String.Empty);

                _repository.Add(newCategory);
                _repository.Save();
                
                return RedirectToAction("Index");
            }

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            int numberOfSubCategory = _repository.List().Where(c => c.ParentID == id).Count();
            if (numberOfSubCategory == 0)
            {
                _repository.Delete(id);
                _repository.Save();
            }
            else
            {
                Session["ErrorMessageDelete"] = true;
            }

            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            return View(_repository.Get(id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Category categoryToUpdate)
        {
            if (ModelState.IsValid)
            {                                                
                categoryToUpdate.FriendlyUrl = categoryToUpdate.FriendlyUrl.Replace(" ", "-");
                categoryToUpdate.FriendlyUrl = Regex.Replace(categoryToUpdate.FriendlyUrl, "[\"$&+,/:;=?@]", String.Empty).ToLower();
                if (categoryToUpdate.ParentID.HasValue && categoryToUpdate.ParentID.Value <= 0)
                    categoryToUpdate.ParentID = null;

                _repository.Update(categoryToUpdate);
                _repository.Save();

                return RedirectToAction("Index");
            }

            return View();
        }

        public ActionResult DeleteButton(Category category)
        {
            ViewData["SubCategoryCount"] = _repository.List().Where(c => c.ParentID == category.CategoryID).Count();
            ViewData["PostCount"] = category.Posts.Count;
            return PartialView("DeleteButton", category);
        }
                
        [ChildActionOnly]
        public ActionResult CategoryComboBox()
        {
            List<CategoryItem> dropDownItems = new List<CategoryItem>();
            List<Category> categories = _repository.List().Where(c => c.ParentID.HasValue == false).ToList();

            CategoryRepository repository = (CategoryRepository)_repository;
            int depth = 0;
            repository.LoadRecursive(categories, ref dropDownItems, ref depth);

            return PartialView("CategoryComboBox", dropDownItems);
        }

        [ChildActionOnly]
        public ActionResult CategoryComboBoxWithSelectedItem(int selectedParentID)
        {
            List<CategoryItem> dropDownItems = new List<CategoryItem>();
            List<Category> categories = _repository.List().Where(c => c.ParentID.HasValue == false).ToList();

            CategoryRepository repository = (CategoryRepository)_repository;
            int depth = 0;
            repository.LoadRecursive(categories, ref dropDownItems, ref depth);

            TempData["SelectedParentID"] = selectedParentID;

            return PartialView("CategoryComboBox", dropDownItems);
        }

        [ChildActionOnly]
        public int CountPosts(int id)
        {            
            return  _repository.Get(id).Posts.Count;
        }
    }
}
